package com.vercon.commonmirror.aspectj;

import android.util.Log;
import android.view.View;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;

/**
 * 全局点击事件间隔一秒，可以通过 @FastClick(ids = {R.id.XX},interval = { 100l }) 指定间隔时间
 */
@Aspect
public class SingleClickAspect {

    public static final long FAST_CLICK_INTERVAL_GLOBAL = 500L;
    //@Pointcut("execution(@com.vercon.commonmirror.aspectj.FastClick * *(..))")
    @Pointcut("execution(void android.view.View.OnClickListener+.onClick(..))")
    public void methodAnnotated() {}

    //    @Pointcut("execution(* android.support.v7.app.AppCompatViewInflater.DeclaredOnClickListener.onClick(..))")
//    public void onClickInXmlPointcuts() {}
//
    @Pointcut("execution(@butterknife.OnClick * *(..))")
    public void butterKnifeClick(){}

    @Around("methodAnnotated() || butterKnifeClick()")
    public void aroundViewOnClick(ProceedingJoinPoint joinPoint) throws Throwable {

        try{
            // 取出JoinPoint的签名
            MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
            // 取出JoinPoint的方法
            Method method = methodSignature.getMethod();

            // 1\. 全局统一的时间间隔
            long interval = FAST_CLICK_INTERVAL_GLOBAL;

            if (method.isAnnotationPresent(FastClick.class)) {
                // 2\. 如果方法使用了@FastClick修饰，取出定制的时间间隔

                FastClick singleClick = method.getAnnotation(FastClick.class);
                interval = singleClick.interval();

                // 取出目标对象
                View target = (View) joinPoint.getArgs()[0];

                //Log.e("SingleClickAspect","FastClick id   " + target.getId());

                int[] ids = singleClick.ids();
                if(ids.length > 0 && ids[0] != 0){
                    for (int i = 0; i < ids.length; i++) {
                        if(target.getId() == ids[i]){
                            // 3\. 根据点击间隔是否超过interval，判断是否为快速点击
                            if (!FastClickCheckUtil.isFastClick(target, interval)) {
                                joinPoint.proceed();
                            }
                        }else {
                            joinPoint.proceed();
                        }
                    }
                }else {
                    if (!FastClickCheckUtil.isFastClick(target, interval)) {
                        joinPoint.proceed();
                    }
                }
            }else {
                //Log.e("SingleClickAspect","FastClick 1000");
                // 取出目标对象
                View target = (View) joinPoint.getArgs()[0];
                // 3\. 根据点击间隔是否超过interval，判断是否为快速点击
                //Log.e("SingleClickAspect"," " + FastClickCheckUtil.isFastClick(target, interval));
                if (!FastClickCheckUtil.isFastClick(target, interval)) {
                    joinPoint.proceed();
                }
            }
        }catch (Exception e){
            joinPoint.proceed();
        }

    }
}